package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "购车控制器")
@Slf4j
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result<String> addCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("[添加购物车] dto:{}",shoppingCartDTO);
        Integer tag = shoppingCartService.add(shoppingCartDTO);
        return Result.success(tag > 0 ? "添加成功":"添加失败");
    }

    @GetMapping("/list")
    public Result<List<ShoppingCart>> findAll(){
        log.info("[获取购物车]");
        List<ShoppingCart> all = shoppingCartService.findAll();
        return Result.success(all);
    }

    @DeleteMapping("/clean")
    public Result<String> clean(){
        log.info("[清空购物车]");
        Integer tag = shoppingCartService.clean();
        return tag>0 ? Result.success(): Result.error("清空失败");
    }



}
